package com.GainLoop.Api;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.GainLoop.Api.Entity.EntityAbstract;

public class ApiResponseArray<T extends EntityAbstract> extends ApiResponseAbstract<ArrayList<T>, T>
{

	public ApiResponseArray(JSONObject response, Class<T> classT) throws JSONException {
		super(response, classT);
	}

	public ApiResponseArray(int retCode, ArrayList<T> data, Class<T> classT) {
		super(retCode, data, classT);
	}

	@Override
	protected ArrayList<T> _getData(JSONObject response) throws JSONException {
		if(!response.has(DATA_KEY)) {
			return null;
		}
		
		JSONArray arrS = response.getJSONArray(DATA_KEY);
		ArrayList<T> arrT = new ArrayList<T>(arrS.length());
		
		for(int i = 0; i < arrT.size(); i++) {
			arrT.set(i, EntityAbstract.fromObject(arrS.getJSONObject(i), getClassT()));
		}
		
		return arrT;
	}
	
}
